package queuedemo;

/**
 * 双链表实现队列
 */
public class Myqueue {
    static class Listcode{
        public int val;
        public Listcode prev;
        public Listcode next;

        public Listcode(int val) {
            this.val = val;
        }
    }
    public Listcode head;
    public Listcode last;
    public int usesized;


    //入队 尾插法
    public void offer(int val){
Listcode listcode=new Listcode(val);
if (head==null){
    head=listcode;
    last=listcode;
}else {
    last.next=listcode;
    listcode.prev=last;
    last=last.next;
}
usesized++;
    }

    //删除头节点
    public int poll(){
if (head==null) return -1;
int val=head.val;
if (head.next==null) {
    head=null;
    last=null;
}else {
    head=head.next;
    head.prev=null;
}
return val;
    }
//    获取头节点的值
    public int peek(){
        if (head==null) return -1;
        return head.val;
    }
    public boolean isEmpty(){
return usesized==0;
    }
    public int size(){
return usesized;
    }
}
